package com.epoint.mobileoa.personalized.epointcompany.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetXMListTask;
import com.epoint.mobileoa.personalized.epointcompany.model.ProjectInfoModel;
import com.epoint.mobileoa.service.MainService;

/**
 * @author liyc
 * @time 2012-4-16 下午6:51:35
 * @annotation 项目选择
 */
public class XMChoose extends SuperView {
	
	private ListView lv;

	private int pagecount = 15;//每页显示数量

	private int pageindex = 1;//第几页

	public static int allxmnums;//所有邮件数量

	private Button load_bt;//加载中的文本
	private LinearLayout foot;
	
	private int visibleItemCount;//一页可显示的List数量
	
	public static ProjectInfoModel choosedProject;
	
	private List<ProjectInfoModel> xmlist = new ArrayList<ProjectInfoModel>();
	
	@Override
	public void initControls() {
		
		lv = (ListView) findViewById(R.id.lv_mailstyle);
		lv.setOnItemClickListener(this);
		
		addFoot();

		getData(pageindex,pagecount);
	}
	
	public void getData(int pageindex, int pagecount){
		top_pb.setVisibility(View.VISIBLE);
		HashMap param=new HashMap();
		
		param.put("pageindex", pageindex);
		param.put("pagecount", pagecount);
		
		Task task = new GetXMListTask(this, param);
		MainService.addTask(task);
	}
	
	public void addFoot(){
		load_bt = new Button(this);
		load_bt.setBackgroundResource(R.drawable.button_login);

		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
		load_bt.setOnClickListener(this);
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			String bs = param[1].toString();
			List<ProjectInfoModel> list = GetXMListTask.XmlAnalysis(bs);
			
			
			for(ProjectInfoModel cim:list){
				xmlist.add(cim);
			}
			MyAdapter adapter = new MyAdapter();
			lv.setAdapter(adapter);
		}
		top_pb.setVisibility(View.GONE);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.xmchooseview;
	}
	
	@Override
	public String setTitle() {
		return "项目选择";
	}
	
	@Override
	public void onClick(View v) {
		if(v==load_bt){
			pageindex++;
			getData(pageindex, pagecount);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		choosedProject = xmlist.get(position);
		finish();
	}
	
	
	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return xmlist.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(XMChoose.this);
				convertView = mInflater.inflate(R.layout.epointxmlist, null);
				holder.ml_mailimg = (ImageView) convertView
						.findViewById(R.id.ml_mailimg);
				holder.ml_attach = (ImageView) convertView
						.findViewById(R.id.ml_attach);
				holder.ml_mailtitle = (TextView) convertView
						.findViewById(R.id.ml_mailtitle);
				holder.ml_sandt = (TextView) convertView
						.findViewById(R.id.ml_sandt);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.ml_mailimg.setVisibility(View.GONE);
			holder.ml_mailtitle.setText(xmlist.get(position).XiangMuMC);
			setTextViewFontColor(holder.ml_mailtitle);
			String sender = xmlist.get(position).XiangMuBH;
			holder.ml_sandt.setText(sender);
			return convertView;
		}
		
	}
	
	public class ViewHolder {
		public ImageView ml_mailimg;
		public ImageView ml_attach;
		public TextView ml_mailtitle;
		public TextView ml_sandt;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}

}
